package com.example.state;

import com.example.Machine;

import java.rmi.RemoteException;

public class SoldState implements State {

    Machine machine;

    public SoldState(Machine machine) {
        this.machine = machine;
    }

    @Override
    public void insertQuarter() {
        System.out.println("正在准备糖果呢，不需要投入硬币~");
    }

    @Override
    public void ejectQuarter() {
        System.out.println("正在准备糖果呢，不能退出硬币~");
    }

    @Override
    public void turnCrank() {
        System.out.println("正在准备糖果呢，不能移动摇杆~");
    }

    @Override
    public void dispense() {

        machine.releaseBall();

        try {
            machine.notifyObservers();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        System.out.println("糖果已经出来了~");
        if (machine.getCount() >= 0) {
            machine.setState(machine.getNoQuarterState());
        } else {
            machine.setState(machine.getSoldOutState());
        }

    }
}
